package com.geekchique.runningbuddy.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.Spinner;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.controller.IMapOfPastJogsController;
import com.geekchique.runningbuddy.controller.MapOfPastJogsControllerImpl;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MapOfPastJogs extends MapActivity {
	
	private IMapOfPastJogsController mapOfJogController;
	public LocationManager locationManager = null;
    protected LocationProvider provider;
	protected MyIntentReceiver myIntentReceiver = null; 
	private static final String PROVIDERNAME = "testjog";
	private static final String TAG = "MAPOFJOG";
	final Handler mHandler = new Handler();
	
  @Override
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      this.setContentView(R.layout.mapview);
      
      locationManager =  (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      
      mapOfJogController = new MapOfPastJogsControllerImpl(this);
      
      mapOfJogController.createMapOfJogView(
    		  (Button)findViewById(R.id.go),
    		  (Spinner)findViewById(R.id.spinner1),
    		  android.R.layout.simple_spinner_dropdown_item,
    		  (MapView)findViewById(R.id.map));
      
  	}
  
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return mapOfJogController.onKeyDown(keyCode, event);
	}
	
	class MyIntentReceiver extends IntentReceiver {

		@Override
		public void onReceiveIntent(Context context, Intent intent) {
			Log.v(TAG,"Updating a jogging a current jogging route.");
			Location location = locationManager.getCurrentLocation(PROVIDERNAME);
			Log.i(TAG, "Updating longitude: "+location.getLongitude() + ", latitude: "+location.getLatitude());
			mapOfJogController.setCurrentGPSLocationOfPhone(location);
			mapOfJogController.updateRunningPath();
		}
	}
	
}